const app = getApp();
const platSheetList = [{
  text: "维修端",
  role: 1
}, {
  text: "销售端",
  role: 2
}, {
  text: "合伙人端",
  role: 3
}];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showActionSheet: false,
    tips: "切换至其他管理端",
    itemList: [],
    user: {},
    modal: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function (options) {
    this.getUserData();

  },

  //跳转到账号管理
  navAccountManage() {
    wx.navigateTo({
      url: '/package/pages/accountMannage/index',
    })
  },


  onHide: function () {
    this.setData({
      showActionSheet: false
    })
  },



  //获取用户账户信息
  getUserData() {
    app.ajax({
      url: 'Manage/User/getManageInfo',
      method: "POST",
      data: {},
      success: res => {
        if (res.data.code == 200) {
          const user = res.data.data;
          user.formatPhone = user.phone.replace(/(\d{3})\d*(\d{4})/, '$1****$2')
          this.setData({
            user
          })
        } else {
          app.showToast("获取用户信息失败");
        }
      }
    })
  },

  //跳转到修改密码界面
  navAlterPassword() {
    wx.navigateTo({
      url: "../alterPwd/alterPwd"
    })
  },

  //切换到其他管理端
  changeOtherPlat() {
    let role = wx.getStorageSync('role')
    if (role >= 1 && role == platSheetList[role - 1].role) {
      platSheetList.splice(role - 1, 1);
    }
    this.setData({
      itemList: platSheetList,
      showActionSheet: true
    })
  },

  closeActionSheet() {
    this.setData({
      showActionSheet: false
    })
  },


  //点击切换其他端的item
  itemClick(e) {
    const {
      index,
      item
    } = e.detail;
    const role = item.role;
    wx.navigateTo({
      url: "/pages/login/login",
      success: (res) => {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', {
          data: role
        })
      }
    })
  },

  //退出登录
  logout() {
    this.setData({
      modal: true
    })

  },


  hide() {
    this.setData({
      modal: false
    })
  },


  //退出modal窗
  handleClick(e) {
    const index = e.detail.index;
    if (index == 0) {
      this.hide();
    } else {

      //删除历史登录信息
      const loaginHistoryRoles = wx.getStorageSync('loginHistoryRoles') || [];
      if (loaginHistoryRoles.length > 0) {
        let index = loaginHistoryRoles.findIndex(item => item.id == this.data.user.id && item.role == wx.getStorageSync('role'));
        if (~index) {
          loaginHistoryRoles.splice(index, 1);
        }
        wx.setStorageSync('loginHistoryRoles', loaginHistoryRoles);
      }

      wx.removeStorageSync('role');
      wx.removeStorageSync('token');
      wx.reLaunch({
        url: '/pages/login/login',
      })
    }

  }


})